import request from './comment'
// interface IType = {
//     id?:number;
//     method:'post'|'get'|'put'|'delete'
// }
const HTTP = function (type, url, data) {
    return new Promise((resolve, reject) => {
        if (arguments.length === 3) {
            switch (type.method) {
                case 'post':
                    if (type.id) {
                        url = url.replace(/:id/g, type.id)
                        request.post(url, data).then(res => resolve(res)).catch(err => reject(err))
                    } else {
                        request.post(url, data).then(res => resolve(res)).catch(err => reject(err))
                    }
                    break;
                case 'get':
                    console.log('sdsadasdas')
                    if (type.id) {
                        url = url.replace(/:id/g, type.id)
                        request.get(url, data).then(res => resolve(res)).catch(err => reject(err))
                    } else {
                        request.get(url, data).then(res => resolve(res)).catch(err => reject(err))
                    }
                    break;

                case 'put':
                    if (type.id) {
                        url = url.replace(/:id/g, type.id)
                        request.put(url, data).then(res => resolve(res)).catch(err => reject(err))
                    } else {
                        request.put(url, data).then(res => resolve(res)).catch(err => reject(err))
                    }
                    break;

                case 'delete':
                    if (type.id) {
                        url = url.replace(/:id/g, type.id)
                        request.delete(url, data).then(res => resolve(res)).catch(err => reject(err))
                    } else {
                        request.delete(url, data).then(res => resolve(res)).catch(err => reject(err))
                    }
                    break;
            }
        } else {
            switch (type.method) {
                case 'post':
                    if (type.id) {
                        url = url.replace(/:id/g, type.id)
                        request.post(url).then(res => resolve(res)).catch(err => reject(err))
                    } else {
                        request.post(url).then(res => resolve(res)).catch(err => reject(err))
                    }
                    break;
                case 'get':
                    console.log('sdsadasdas')
                    if (type.id) {
                        url = url.replace(/:id/g, type.id)
                        request.get(url).then(res => resolve(res)).catch(err => reject(err))
                    } else {
                        request.get(url).then(res => resolve(res)).catch(err => reject(err))
                    }
                    break;

                case 'put':
                    if (type.id) {
                        url = url.replace(/:id/g, type.id)
                        request.put(url).then(res => resolve(res)).catch(err => reject(err))
                    } else {
                        request.put(url).then(res => resolve(res)).catch(err => reject(err))
                    }
                    break;

                case 'delete':
                    if (type.id) {
                        url = url.replace(/:id/g, type.id)
                        request.delete(url).then(res => resolve(res)).catch(err => reject(err))
                    } else {
                        request.delete(url).then(res => resolve(res)).catch(err => reject(err))
                    }
                    break;
            }
        }
    })
}


export default HTTP